package com.rg.servlet;

import com.rg.model.UserModel;
import com.rg.service.UserRegisterService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UserRegisterServlet")
public class UserRegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName=req.getParameter("username");
        String userPwd=req.getParameter("password");
        String Name=req.getParameter("name");
        String email=req.getParameter("email");
        String phone=req.getParameter("phone");
        String address=req.getParameter("address");
        UserModel user=new UserModel(null,userName,userPwd,Name,email,phone,address,0);
        UserRegisterService userRegisterService=new UserRegisterService();
        boolean a=userRegisterService.saveUser(user);
        String registerInfo=null;
        if (a){
            registerInfo="<label style='color:green'>恭喜您注册成功！3 秒之后自动跳转至登录页面</label>";
            req.setAttribute("registerInfo",registerInfo);
            resp.setHeader("Refresh","3,url=login.jsp");
            req.getRequestDispatcher("register.jsp").forward(req,resp);
        }else {
            registerInfo="<label>请重新注册！</label>";
            req.setAttribute("registerInfo",registerInfo);
            resp.sendRedirect("register.jsp");
        }
    }
}
